﻿@model IEnumerable<Microsoft.Gateway.Utils.Update.PackageManifest>
@using Microsoft.Gateway.Utils
@using Microsoft.Gateway.Utils.Update
@{
    ViewBag.Title = "Update Management Console Application";
}

<h2>Available Management Console Updates</h2>
<table class="table">
    <tr>
        <th>

        </th>
        <th>
            Version
        </th>
        <th>
            Severity
        </th>
        <th>
            Description
        </th>
    </tr>

    @foreach (var update in Model)
    {
        string rowClass = "info";
        switch ((UpdateSeverity)update.Severity)
        {
            case UpdateSeverity.Critical:
                rowClass = "danger";
                break;

            case UpdateSeverity.Important:
                rowClass = "warning";
                break;
        }
        <tr class="@rowClass">
            <td>
                <a class="applyLink" href="#" onclick="applyUpdate('@VersionUtils.GatewayFormat(update.Version)')">Apply Update</a>
            </td>
            <td>
                @VersionUtils.GatewayFormat(update.Version)
            </td>
            <td>
                @update.Severity
            </td>
            <td>
                @Html.Raw(update.Description)
            </td>
        </tr>
    }
</table>
@section Scripts {
    <script type="text/javascript">

        function applyUpdate(version) {
            if (confirm('You have selected to upgrade this Management Console site to version: ' + version + '\n\nThis operation cannot be undone.\n\nAre you sure you want to continue?')) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("Update")',
                    data: { version: version},
                    error: function (xhr, status, error) {
                        alert('The application failed to update: ' + error);
                    },
                    success: function (response) {
                        if (response.Error !== '') {
                            alert('The application failed to update: ' + response.Error);
                        }
                        else {
                            alert('The application update is in progress...');
                            location.href = '@Url.Action("Index")';
                        }
                    }
                });
            }
            return false;
        }

    </script>
}
